package com.wp.aop01.service.aspect;

import com.sun.xml.internal.ws.spi.db.MethodGetter;
import com.wp.aop01.service.annotation.MyPremission;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.io.OutputStream;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * @author: WangPeng
 * @createTime: 2022/11/12 14:34
 * @description:
 * @version: v1.0
 */
@Component
@Aspect
public class PermissionAdvice {
    //表示所有使用该注解的方法为切点方法
    @Pointcut("@annotation(com.wp.aop01.service.annotation.MyPremission)")
    public void permissionPointcut(){}

    private List<String> Permission = new ArrayList<>();

    public PermissionAdvice() {
        Permission.add("list");
        Permission.add("delete");
        Permission.add("update");
    }

    @Around("permissionPointcut()")
    public void  before(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        MyPremission annotation = method.getAnnotation(MyPremission.class);
        if (annotation != null){
            String value = annotation.value();
//            System.out.println(value);
            if (!Permission.contains(value)){
                System.out.println("权限不足");
            }
        }
//        System.out.println("权限校验");
    }
}
